﻿function get-xml($path) {
    return [xml] [string]::join(”`n”, (gc $path))
}


<# 
    Parses fields of a Create-Statement
    Lines eg.: [ShipmentValueCurrency] [nvarchar](10) NULL,
#>
function Parse([string] $path){
    if(!( Test-path $path) ){
	   write-host Directory $path is not available!
       return
    } 
    
    $xml= get-xml $path
      
    foreach ($row in $xml.Root.row){
        "ID " + $row.RowID
    }
    
        
    return $xml
}


function BuildInsert($fieldInfos, $tableName){
    $statement = New-Object System.Collections.ArrayList
    $sink = $statement.Add("create procedure [RenameMe] (")
    for ($i = 0; $i -lt $fieldInfos.Count; $i ++) {
        $token = "`t@" + $fieldInfos[$i].Name + " " + $fieldInfos[$i].Type 
        if($i -lt $fieldInfos.Count - 1){
            $token = $token  + ","
        }
        $sink = $statement.Add($token)
    }
    $sink = $statement.Add(")")
    $sink = $statement.Add("as")
    $sink = $statement.Add("begin")
    $sink = $statement.Add("insert into " + $tableName)
    for ($i = 0; $i -lt $fieldInfos.Count; $i ++) {
        $token = "`t"  + $fieldInfos[$i].Name
          if($i -lt $fieldInfos.Count - 1){
            $token = $token  + ","
        }
        $sink = $statement.Add($token)
    }
    $sink = $statement.Add("values (")
    for ($i = 0; $i -lt $fieldInfos.Count; $i ++) {
        $token = "`t@" + $fieldInfos[$i].Name
         if($i -lt $fieldInfos.Count - 1){
            $token = $token  + ","
        }
        $sink = $statement.Add($token)
    }
    $sink = $statement.Add(");")
    $sink = $statement.Add("end")
    return  $statement
}





#. .\sql.ps1

 Parse "D:\Scripting\Ps\SenditPrinted.xml"
